

#import "PreviewItemAndComponentView.h"
#import "PreviewItemsCell.h"
#import "PreviewComponentsCell.h"
#import "PreviewCompsModel.h"
#import "PreviewItemsModel.h"
#import "ICMRepairMaintenanceModel.h"
#import "ICMRepairPartsModel.h"

@interface PreviewItemAndComponentView()<UITableViewDelegate,UITableViewDataSource>
//项目
@property (nonatomic, strong) UITableView *itemsTableView;
//零件
@property (nonatomic, strong) UITableView *componentsTableView;

//项目数据源
@property (nonatomic, strong) NSArray *itemsModels;
//零件数据源
@property (nonatomic, strong) NSArray *componentsModels;


//View类型(一般维修/常规保养)
@property (nonatomic,copy) NSString* type;

//项目TabView起始位置
@property (nonatomic) int itemTabViewDis;
//零件TabView起始位置
@property (nonatomic) int compTabViewDis;

@property (nonatomic,assign) BOOL isHideHeader;
@end

@implementation PreviewItemAndComponentView
+(instancetype)creatWithFrame:(CGRect)frame andItems:(NSArray *)items andComps:(NSArray *)comps andType:(NSString *)type{
    return [[PreviewItemAndComponentView alloc]initWithFrame:frame andItems:items andComps:comps
                                                     andType: type];
    
}
-(instancetype)initWithFrame:(CGRect)frame andItems:(NSArray *)items andComps:(NSArray *)comps andType:(NSString *)type{
    self.type = type;
    self = [super initWithFrame:frame];
    
    if(self){
        self.itemsModels = items;
        self.componentsModels = comps;
        [self setUpViews];
    }
    
    return self;
    
}
- (NSArray *)itemsModels{
    if(!_itemsModels){
        
        _itemsModels = [[NSArray alloc]init];
    }
    return _itemsModels;
    
}
- (NSArray *)componentsModels{
    if(!_componentsModels){
        
        _componentsModels = [[NSArray alloc]init];
    }
    return _componentsModels;
    
}
-(void)setUpViews{
    
    if([self.type isEqualToString:@"活动"]){
        self.isHideHeader = YES;
    }
    //一般维修/常规保养
    UILabel *fix  = [[UILabel alloc] initWithFrame:CGRectMake(ActualWidth(35), ActualHeight(30), ScreenWidth, ActualHeight(35))];
    fix.textAlignment = NSTextAlignmentLeft;
    fix.textColor = TitleColor;
    fix.font = NaviFont;
    fix.text = self.type;
    fix.backgroundColor = Color(0xf0f0f0);
    
    
    if(self.isHideHeader ==  NO)
        [self addSubview:fix];
    
    //项目图片
    UIImage *projImg = [UIImage imageNamed:GetCar_Items];
    UIImageView *projImgView = [[UIImageView alloc]initWithImage:projImg];
    CGFloat verticalDistance;
    if(self.itemsModels.count == 0)
        verticalDistance = fix.bottomEdge+ActualHeight(20);
    else
        verticalDistance = fix.bottomEdge + (self.itemsModels.count*ActualHeight(80) +ActualHeight(80))/2-ActualHeight(50);
    
    projImgView.frame =CGRectMake(ActualHeight(20), verticalDistance, ActualWidth(40), ActualHeight(60));
    
    //设置图片的显示模式
    projImgView.contentMode = UIViewContentModeScaleAspectFill;
    
    [self addSubview:projImgView];
    
    
    
    //项目tableView title
    //项目名称/代码
    UILabel *projNameCodeTitle  = [[UILabel alloc] initWithFrame:CGRectMake(ActualWidth(110), fix.bottomEdge + ActualHeight(15), ActualWidth(340), ActualHeight(20))];
    projNameCodeTitle.textAlignment = NSTextAlignmentLeft;
    projNameCodeTitle.textColor = [UIColor blackColor];
    projNameCodeTitle.text = @"项目名称/代码";
    [self addSubview:projNameCodeTitle];
    
    
    //执行工时
    UILabel *workTimes  = [[UILabel alloc] initWithFrame:CGRectMake(projNameCodeTitle.rightEdge, fix.bottomEdge + ActualHeight(15), ActualWidth(140), ActualHeight(20))];
    workTimes.textAlignment = NSTextAlignmentLeft;
    workTimes.textColor = [UIColor blackColor];
    workTimes.text = @"执行工时";
    if(self.isHideHeader ==  NO)
        [self addSubview:workTimes];
    
    //工时单价
    UILabel *workPrice  = [[UILabel alloc] initWithFrame:CGRectMake(workTimes.rightEdge, fix.bottomEdge + ActualHeight(15), ActualWidth(140), ActualHeight(20))];
    workPrice.textAlignment = NSTextAlignmentLeft;
    workPrice.textColor = [UIColor blackColor];
    workPrice.text = @"工时单价";
    if(self.isHideHeader ==  NO)
        [self addSubview:workPrice];
    
    //工时费
    UILabel *workprice  = [[UILabel alloc] initWithFrame:CGRectMake(workPrice.rightEdge, fix.bottomEdge + ActualHeight(15), ActualWidth(140), ActualHeight(20))];
    workprice.textAlignment = NSTextAlignmentLeft;
    workprice.textColor = [UIColor blackColor];
    workprice.text = @"工时费(折前)";
    if(self.isHideHeader ==  NO)
        [self addSubview:workprice];
    
    //折扣率
    UILabel *dsicount  = [[UILabel alloc] initWithFrame:CGRectMake(workprice.rightEdge+50, fix.bottomEdge + ActualHeight(15), ActualWidth(140), ActualHeight(20))];
    dsicount.textAlignment = NSTextAlignmentLeft;
    dsicount.textColor = [UIColor blackColor];
    dsicount.text = @"折扣率";
    if(self.isHideHeader ==  NO)
        [self addSubview:dsicount];
    
    //第二行线
    UIView *secondLine = [[UIView alloc] initWithFrame:CGRectMake(ActualWidth(100), workprice.bottomEdge+ActualHeight(10), ActualWidth(1024)-ActualWidth(100), 1)];
    
    secondLine.backgroundColor = BackgroundColor_Gray;
    
    [self addSubview:secondLine];
    
    //设置项目tableview
    self.itemTabViewDis = secondLine.bottomEdge;
    [self addSubview:self.itemsTableView];
    
    float total  = 0;
    for (ICMRepairMaintenanceModel *model in self.itemsModels) {
        total += [model.LABOUR_AMOUNT doubleValue]?:0;
//        total += [model.STD_LABOUR_HOUR floatValue]*[[self calculateWorkingHoursWithSERIES_LABOUR_PRICE:model.SERIES_LABOUR_PRICE andREPAIR_ITEM_TYPE_RATE:model.REPAIR_ITEM_TYPE_RATE andTECHNIC_MODULUS:model.TECHNIC_MODULUS] doubleValue]?:0;
    }
    //item小计
    UILabel *totalItem  = [[UILabel alloc] initWithFrame:CGRectMake(ActualWidth(764), self.itemsTableView.bottomEdge + ActualHeight(10), ActualWidth(260), ActualHeight(25))];
    totalItem.textAlignment = NSTextAlignmentLeft;
    totalItem.textColor = ContentTextColor_Gray;
    totalItem.text = [NSString stringWithFormat:@"小计:%.2f元",total];
    if(self.isHideHeader ==  NO)
        [self addSubview:totalItem];
    
    
    for (ICMRepairMaintenanceModel *model in self.itemsModels) {
        if ([model.REPAIR_TYPE_NAME isEqualToString:@"管理工位"]) {
            
        }else{
            
            
            //项目与零件分割线
            UIView *sperateLine = [[UIView alloc] initWithFrame:CGRectMake(ActualWidth(5), totalItem.bottomEdge+ActualHeight(30), ActualWidth(1024)-ActualWidth(5), 1)];
            
            sperateLine.backgroundColor = BackgroundColor_Gray;
            
            [self addSubview:sperateLine];
            
            
            
            
            //零件图片
            UIImage *compsImg = [UIImage imageNamed:GetCar_Parts];
            
            UIImageView *compsImgView = [[UIImageView alloc]initWithImage:compsImg];
            
            CGFloat verticalDistanceComps;
            if(self.componentsModels.count == 0)
                verticalDistanceComps = sperateLine.bottomEdge + ActualHeight(20);
            else
                verticalDistanceComps = sperateLine.bottomEdge + (self.componentsModels.count*ActualHeight(80) +ActualHeight(80))/2-ActualHeight(50);
            
            compsImgView.frame =CGRectMake(ActualHeight(20), verticalDistanceComps, ActualWidth(40), ActualHeight(60));
            
            //设置图片的显示模式
            projImgView.contentMode = UIViewContentModeScaleAspectFill;
            
            [self addSubview:compsImgView];
            
            
            
            
            //零件名称代码
            UILabel *compNameCodeTitle  = [[UILabel alloc] initWithFrame:CGRectMake(ActualWidth(110), sperateLine.bottomEdge + ActualHeight(15), ActualWidth(200), ActualHeight(20))];
            compNameCodeTitle.textAlignment = NSTextAlignmentLeft;
            compNameCodeTitle.textColor = [UIColor blackColor];
            compNameCodeTitle.text = @"零件名称/代码";
            [self addSubview:compNameCodeTitle];
            
            
            //数量
            UILabel *num  = [[UILabel alloc] initWithFrame:CGRectMake(compNameCodeTitle.rightEdge, sperateLine.bottomEdge + ActualHeight(15), ActualWidth(140), ActualHeight(20))];
            num.textAlignment = NSTextAlignmentLeft;
            num.textColor = [UIColor blackColor];
            num.text = @"数量";
            [self addSubview:num];
            
            //零件单价
            UILabel *perPrice  = [[UILabel alloc] initWithFrame:CGRectMake(num.rightEdge, sperateLine.bottomEdge + ActualHeight(15), ActualWidth(140), ActualHeight(20))];
            perPrice.textAlignment = NSTextAlignmentLeft;
            perPrice.textColor = [UIColor blackColor];
            perPrice.text = @"零件单价";
            if(self.isHideHeader ==  NO)
                [self addSubview:perPrice];
            
            //零件金额
            UILabel *price  = [[UILabel alloc] initWithFrame:CGRectMake(perPrice.rightEdge, sperateLine.bottomEdge + ActualHeight(15), ActualWidth(140), ActualHeight(20))];
            price.textAlignment = NSTextAlignmentLeft;
            price.textColor = [UIColor blackColor];
            price.text = @"零件金额(折前)";
            if(self.isHideHeader ==  NO)
                [self addSubview:price];
            
            
            //折扣率
            UILabel *partDsicount  = [[UILabel alloc] initWithFrame:CGRectMake(price.rightEdge+50, sperateLine.bottomEdge + ActualHeight(15), ActualWidth(140), ActualHeight(20))];
            partDsicount.textAlignment = NSTextAlignmentLeft;
            partDsicount.textColor = [UIColor blackColor];
            partDsicount.text = @"折扣率";
            if(self.isHideHeader ==  NO)
                [self addSubview:partDsicount];
            
            
            
            
            //零件属性
            UILabel *prop  = [[UILabel alloc] initWithFrame:CGRectMake(partDsicount.rightEdge, sperateLine.bottomEdge + ActualHeight(15), ActualWidth(140), ActualHeight(20))];
            prop.textAlignment = NSTextAlignmentLeft;
            prop.textColor = [UIColor blackColor];
            prop.text = @"零件属性";
            if(self.isHideHeader ==  NO)
                [self addSubview:prop];
            
            //第四行线
            UIView *fourthLine = [[UIView alloc] initWithFrame:CGRectMake(ActualWidth(100), prop.bottomEdge+ActualHeight(10), ActualWidth(1024)-ActualWidth(100), 1)];
            
            fourthLine.backgroundColor = BackgroundColor_Gray;
            
            
            [self addSubview:fourthLine];
            
            //设置零件tableview
            self.compTabViewDis = fourthLine.bottomEdge;
            
            [self addSubview:self.componentsTableView];
            
            
            
            total = 0;
            for (ICMRepairPartsModel *model in self.componentsModels) {
                total += [model.PART_SALES_AMOUNT floatValue];
            }
            //comp小计
            UILabel *totalComp = [[UILabel alloc] initWithFrame:CGRectMake(ActualWidth(764), self.componentsTableView.bottomEdge + ActualHeight(10), ActualWidth(260), ActualHeight(25))];
            totalComp.textAlignment = NSTextAlignmentLeft;
            totalComp.textColor = ContentTextColor_Gray;
            totalComp.text = [NSString stringWithFormat:@"小计:%.2f元",total];
            if(self.isHideHeader ==  NO)
                [self addSubview:totalComp]; 
        }
    }

    
    
    
    

}




-(BOOL)isItems:(UITableView *)tableView{
    if(tableView == self.itemsTableView)
        return YES;
    else
        return NO;
}
/*tableViewDelegate tableViewDataSource*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if([self isItems:tableView])
        return self.itemsModels.count;
    else
        return self.componentsModels.count;
    
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if([self isItems:tableView]){
        
        PreviewItemsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PreviewItemsCell"];
        if(cell == nil){
            NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"PreviewItemsCell" owner:nil options:nil];
            cell = [nibs lastObject];
            
        }
        if(self.isHideHeader == YES){
            cell.time.hidden  = YES;
            cell.price.hidden  = YES;
        }
        
        ICMRepairMaintenanceModel *maintenanceModel = self.itemsModels[indexPath.row];
        
        [cell setViewWithObject:maintenanceModel];

//        [cell setViewWithObject:self.itemsModels[indexPath.row]];
        
        return cell;
        
    }else{
        
        PreviewComponentsCell* cell = [tableView dequeueReusableCellWithIdentifier:@"PreviewComponentsCell"];
        if(!cell){
            NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"PreviewComponentsCell" owner:nil options:nil];
            cell = [nibs lastObject];
        }
        
        if(self.isHideHeader == YES){
            
            cell.perPrice.hidden = YES;
            cell.totalPrice.hidden = YES;
            cell.compType.hidden = YES;
            
        }
        
        
        [cell setViewWithObject:self.componentsModels[indexPath.row]];
        return cell;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ActualHeight(80);
}

- (UITableView *)itemsTableView{
    if (!_itemsTableView) {
        _itemsTableView = [[UITableView alloc]initWithFrame:CGRectMake(ActualWidth(100), self.itemTabViewDis, self.frame.size.width, self.itemsModels.count*ActualHeight(80)+2)];
        _itemsTableView.delegate = self;
        _itemsTableView.dataSource = self;
        _itemsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_itemsTableView registerNib: [UINib nibWithNibName:@"PreviewItemsCell" bundle:nil]
              forCellReuseIdentifier:@"PreviewItemsCell"];
        _itemsTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        _itemsTableView.tableFooterView.backgroundColor = [UIColor whiteColor];
        _itemsTableView.bounces = NO;
        //[self addSubview:_itemsTableView];
    }
    return _itemsTableView;
}

- (UITableView *)componentsTableView{
    if (!_componentsTableView) {
        _componentsTableView = [[UITableView alloc]initWithFrame:CGRectMake(ActualWidth(100), self.compTabViewDis, self.frame.size.width, self.componentsModels.count*ActualHeight(80)+2)];
        _componentsTableView.delegate = self;
        _componentsTableView.dataSource = self;
        _componentsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_componentsTableView registerNib:[UINib nibWithNibName:@"PreviewComponentsCell" bundle:nil]
                   forCellReuseIdentifier:@"PreviewComponentsCell"];
        _componentsTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        _componentsTableView.tableFooterView.backgroundColor = [UIColor whiteColor];
        _componentsTableView.bounces = NO;
        // [self addSubview:_componentsTableView];
    }
    return _componentsTableView;
}

//-(NSString *)calculateWorkingHoursWithSERIES_LABOUR_PRICE:(NSString *)SERIES_LABOUR_PRICE andREPAIR_ITEM_TYPE_RATE:(NSString *)REPAIR_ITEM_TYPE_RATE andTECHNIC_MODULUS:(NSString *)TECHNIC_MODULUS{
//    
//    double series_labour_price = [SERIES_LABOUR_PRICE floatValue];
//    double repair_item_type_rate = [REPAIR_ITEM_TYPE_RATE floatValue];
//    double texhnic_modulus = [TECHNIC_MODULUS floatValue];
//    
//    double workingHours = series_labour_price * repair_item_type_rate * texhnic_modulus;
//    
//    NSString *workingHoursStr = [NSString stringWithFormat:@"%.2f",workingHours];
//    
//    return workingHoursStr;
//}



@end
